package com.czx.greendaodemo;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.rx.RxDao;
import org.greenrobot.greendao.rx.RxQuery;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

public class RXActivity extends AppCompatActivity {


    DaoSession daoSession;
    RxDao<Video, Long> videoDao;
    RxQuery<Video> videosQuery;
    List<Video> list;
    RVAdapter rvAdapter;

    @BindView(R.id.et)
    EditText et;
    @BindView(R.id.add)
    Button add;
    @BindView(R.id.delete)
    Button delete;
    @BindView(R.id.search)
    Button search;
    @BindView(R.id.rv_show)
    RecyclerView rvShow;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        rvShow = (RecyclerView) findViewById(R.id.rv_show);
        rvAdapter = new RVAdapter(this, null);
        rvShow.setLayoutManager(new LinearLayoutManager(this));
        rvShow.setAdapter(rvAdapter);

        daoSession = ((MyApplication) getApplication()).getDaoSession();
        videoDao = daoSession.getVideoDao().rx();

        videosQuery = daoSession.getVideoDao().queryBuilder().orderAsc(VideoDao.Properties.Id).rx();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void search() {
        RxQuery<Video> idQuery = daoSession.getVideoDao().queryBuilder().where(VideoDao.Properties.Id.eq(et
                .getText().toString())).rx();
        idQuery.list()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<List<Video>>() {
                    @Override
                    public void call(List<Video> videos) {
                        rvAdapter.updateList(videos);
                    }
                });

    }

    private void delete() {
        Long id = Long.parseLong(et.getText().toString());
        videoDao.deleteByKey(id)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        updateVideo();
                    }
                });

    }

    private void updateVideo() {
        videosQuery.list()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<List<Video>>() {
                    @Override
                    public void call(List<Video> videos) {
                        rvAdapter.updateList(videos);
                    }
                });
    }

    void add() {
        String path = et.getText().toString();
        if (!TextUtils.isEmpty(path)) {
            Video video = new Video(null, path, 0);
            videoDao.insert(video)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1<Video>() {
                        @Override
                        public void call(Video video) {
                            updateVideo();
                        }
                    });
        }
    }

    @OnClick({R.id.add, R.id.delete, R.id.search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.add:
                add();
                break;
            case R.id.delete:
                delete();
                break;
            case R.id.search:
                search();
                break;
        }
    }
}
